<template>
    <div v-loading="loading"  style="height: 100%; overflow-y: scroll">
    <iframe
            v-if="wordId&&(this.isEdit||this.url)"
            :src="url"
            width="100%"
            height="100%"
            frameborder="0"
            id="myIframe"
            name="myIframe"
    ></iframe>
    <div v-else-if="wordId">
<!--        <el-button type="primary" plain v-if="urlList.length&&!hideDownloadBtn" style="float: right; margin-right: 10px" @click="download">正文下载</el-button>-->
        <img v-for="(imgUrl,index) in urlList" :src="imgUrl" width="100%" :key="index" >
    </div>
    <img v-else :src="imageUrl" width="100%">
    </div>
</template>
<script>
    import eventBus from "@/utils/eventBus";
    import schoolSignature from "@/api/officialDocument/schoolSignature/index";
    export default {
        name: "MyWord",
        data() {
            return {
                url: "",
                wordId: "",
                urlList: [],
                loading:false,
            }
        },
        props: {
            row:{
                type:Object,
                default:function () {
                    return {}
                }
            },
            type: {
                type: Number,
                defalut: ""
            },
            isEdit: {
                type: Boolean,
                defalut: false
            },
            hideDownloadBtn:{
                type: Boolean,
                defalut: false
            }
        },
        created() {
            this.id=this.row.text_file
            this.getWord(this.id)
            eventBus.$on("openWord", (id) => {
                this.getWord(id)
            });
            eventBus.$on("reloadView", () => {
                this.reloadView()
            });
        },
        computed: {
            imageUrl() {
                return require("@/assets/images/ss-right-bg.jpg")
            }
        },
        watch: {
            id() {
                this.getWord(this.id)
            }
        },
        methods: {
            download(){
                    window.open(process.env.VUE_APP_BASE_API +"/api/document/"+this.row.id+"/download/?title="+this.row.title)
            },
            previewDocument(obj){
                this.loading = true
                schoolSignature.previewDocument(this.row.id,obj).then(res => {
                    if(res.data.url){
                        this.url = res.data.url
                    }else {
                        this.urlList = res.data
                        if(this.urlList.length==0){
                            this.msgError("预览数据为空，请重新上传！")
                        }
                        this.$emit("update:isEdit",false)
                        this.url = ""
                    }
                    this.loading = false
                }).catch(()=>{
                    this.loading = false
                })
            },
            getWord(id) {
                this.wordId = id
                if (this.wordId) {
                    this.loading = true
                    if (this.isEdit) {
                        schoolSignature.documentFileWopi(this.wordId).then(res => {
                            this.url = res.data.url
                            this.loading = false
                        }).catch(()=>{
                            this.loading = false
                        })
                    } else {
                          schoolSignature.getDocumentFileWopi(this.wordId).then(res => {
                              if(res.data.url){
                                  this.url = res.data.url
                              }else {
                                  this.urlList = res.data
                                  if(this.urlList.length==0){
                                      this.msgError("预览数据为空，请重新上传！")
                                  }
                              }
                              this.loading = false
                          }).catch(()=>{
                              this.loading = false
                          })
                    }
                }
            },
            reloadView(){
                // schoolSignature.getDocumentFileView(this.row.id).then(res => {
                //     if(res.data.url){
                //         this.url = res.data.url
                //     }else {
                //         this.urlList = res.data
                //         if(this.urlList.length==0){
                //             this.msgError("预览数据为空，请重新上传！")
                //         }
                //     }
                //     this.loading = false
                // }).catch(()=>{
                //     this.loading = false
                // })
            }
        },
        beforeDestroy() {
            eventBus.$off("openWord")
            eventBus.$off("reloadView")
        }
    }
</script>
